//
//  YYLHomeTableViewController.swift
//  XinlangWeibo
//
//  Created by yangyinglei on 2017/3/26.
//  Copyright © 2017年 yangyinglei. All rights reserved.
//

import UIKit
import YYModel

class YYLHomeTableViewController: YYLVisitorTableViewController{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !isLogin {
        visitorView?.updateVisitorInfo(imgName: nil, txt: nil)
        
        } else {
        loadData()
        setupUI()
            
        }
        
    }
    
    func setupUI() {
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 200
        
        tableView.register(YYLHomeTableViewCell.self, forCellReuseIdentifier: HomeTabelViewCell)
        tableView.separatorStyle = .none
        //设置上拉刷新视图
        tableView.tableFooterView = pullUpView
        //设置下拉刷新视图
        self.refreshControl = pullDownView
        
        
        //设置tip提示框
        
        if let nav = self.navigationController {
        nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
        
        tipLabel.frame = CGRect(x: 0, y: nav.navigationBar.frame.maxY - 35, width: (self.navigationController?.view.width)!, height: 35)
        }
    }
    
    
    func loadData() {

        homeVM.requestHomeData (isPullUp: pullUpView.isAnimating){ (isSuccess, message) in
            
            if !self.pullUpView.isAnimating  {
                self.tipAnimaton(message: message)
            }
            
            //停止刷新
            self.stopRefreshing()
            
            if isSuccess {
                self.tipLabel.text = message

            //数据请求正常
            self.tableView .reloadData()
            }
        }
       
    }
    //停止刷新的方法
    fileprivate func stopRefreshing() {
    pullUpView.stopAnimating()
    pullDownView.endRefreshing()
    }
    //MARK:------数据源方法
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return homeVM.originalVMArray.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =  tableView.dequeueReusableCell(withIdentifier: HomeTabelViewCell, for: indexPath) as! YYLHomeTableViewCell
        
        let originalVM = homeVM.originalVMArray[indexPath.row]
        cell.originalVM = originalVM
        
        return cell
    }
    //当将要显示最后一行的时候  并且刷新没在刷新    的时候刷新
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        if indexPath.row == homeVM.originalVMArray.count - 1 && !pullUpView .isAnimating {
        pullUpView.startAnimating()
        
        loadData()
        }
    }
    // 下拉刷新动作实现
    func pullDownAction() {
    
            loadData()

    }
    
    //tip动画 
    fileprivate func tipAnimaton(message: String) {
        //如果正在显示 表示在执行动画  就不再次执行了
        if !tipLabel.isHidden {
        return
        
        }
    
    UIView.animate(withDuration: 2, animations: {
        self.tipLabel.isHidden = false
        self.tipLabel.transform = CGAffineTransform(translationX: 0, y: self.tipLabel.height)
    }) { (_) in
        
        UIView.animate(withDuration: 2, animations: { 
            
            self.tipLabel.transform = CGAffineTransform.identity
        }, completion: { (_) in
            self.tipLabel.isHidden = true
        })
        
        }
    
    }
    
    //MARK:------懒加载各个控件
    //homeVM模型 获取数据
    lazy var homeVM: YYLHomeViewModel = YYLHomeViewModel()
    //上拉加载视图
    fileprivate lazy var pullUpView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
        view.color = UIColor.red
        
        return view
    }()
    //下拉刷新视图
    lazy var pullDownView: UIRefreshControl = {
        let refreshC = UIRefreshControl()
        refreshC.addTarget(self, action: #selector(pullDownAction), for: .valueChanged)
        return refreshC
    }()
    
    //提示tip告知视图
    
    lazy var tipLabel: UILabel = {
        let label = UILabel(text: "没有加载的微博数据", font: 14, color: UIColor.red)
        label.backgroundColor = UIColor.orange
        label.textAlignment = .center
        label.isHidden = true
        return label
    }()
    
}
